package dq.study.concurrent.lock;

import dq.study.concurrent.threadpool.MyTask;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author dongqun
 * @version 1.0
 * @description 类的功能描述
 * @date 2019/11/27 11:04
 */
public class ReentrantLockTest {

    public static ReentrantLock lock = new ReentrantLock();
    public static ReentrantLock lock2 = new ReentrantLock();
    public static Integer count = 0;
    public static void increase(){
        lock.lock();
        try {
            System.out.println(count++);
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new MyThread());
        Thread thread2 = new Thread(new MyThread());
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println("-----:"+count);
//        Thread thread1 = new TestThread();
//        Thread thread2 = new TestThread();
//        thread1.start();
//        thread2.start();
    }

    private static class MyThread implements Runnable{

        @Override
        public void run() {
            ReentrantLockTest.increase();
        }
    }

   static class TestThread extends Thread{
        @Override
        public void run() {
            while (true){
                try {
                    ReentrantLockTest.lock2.lock();
                    System.out.println(Thread.currentThread().getName()+"获得锁");
                } finally {
                    ReentrantLockTest.lock2.unlock();
                }

            }

        }
    }


}
